package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.mapper.UserMapper;
import com.j283_5.wisdom_park.pojo.Roleinfo;
import com.j283_5.wisdom_park.pojo.User;
import com.j283_5.wisdom_park.service.UserService;
import com.j283_5.wisdom_park.vo.confcenter_management.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public ResultData getuserInfo(Page<User> page, String usName) {
        if(page==null){
            page = new Page<>(1,10);
        }
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("us_state",0);
        userQueryWrapper.orderByDesc("us_createtime");
        if (usName!=null){
            userQueryWrapper.like("us_name",usName);
        }
        Page<User> userPage=userMapper.selectPage(page,userQueryWrapper);
        if (userPage!=null){
            return ResultData.ok().data(userPage);
        }else {
            return ResultData.ok().data("查询失败!");
        }
    }

    @Override
    public ResultData addUserInfo(UserVo userVo) {
        User user=new User();
        BeanUtils.copyProperties(userVo,user);
        int i = userMapper.insert(user);
        if (i>0){
            return ResultData.ok().data("添加成功!");
        }else {
            return ResultData.ok().data("添加失败!");
        }
    }

    @Override
    public ResultData deleteUser(ArrayList<Integer> usIdlist) {
        UpdateWrapper<User> userUpdateWrapper=new UpdateWrapper<>();
        userUpdateWrapper.set("us_state",1);
        userUpdateWrapper.in("us_id",usIdlist);
        int i=userMapper.update(null,userUpdateWrapper);
        if (i>0){
            return ResultData.ok().message("删除成功!");
        }
        return ResultData.ok().message("删除失败!");
    }

    @Override
    public ResultData updateUser(User user) {
        UpdateWrapper<User> userUpdateWrapper=new UpdateWrapper<>();
        userUpdateWrapper.eq("us_id",user.getUsId())
                .set("us_name",user.getUsName())
                .set("us_phone",user.getUsPhone())
                .set("us_idcard",user.getUsIdcard())
                .set("us_email",user.getUsEmail())
                .set("us_createtime",user.getUsCreatetime());
        int i=userMapper.update(null,userUpdateWrapper);
        if (i>0){
            return ResultData.ok().message("修改成功!");
        }
        return ResultData.ok().message("修改失败!");
    }
}
